
"use strict";
//二级导航判断;
var rolePermission = function(){  
     var _this = this; 
	 //左侧导航选项映射
     _this.firstNav ={
	     "first":[
	         {title:"基本信息",href:"javascript:location.href='/Lvscore_Advertiser_Maven/advertiserWeb/views/userCenter/basicInfo/index.html?'+escape('selectedNav=0&navPos=0')"},
	         {title:"账户余额",href:"javascript:location.href='/Lvscore_Advertiser_Maven/advertiserWeb/views/userCenter/accountBalance/index.html?'+escape('selectedNav=0&navPos=1')"},
	         {title:"财务中心",href:"javascript:location.href='/Lvscore_Advertiser_Maven/advertiserWeb/views/userCenter/financeCenter/index.html?'+escape('selectedNav=0&navPos=2')"},
	         {title:"账户充值",href:"javascript:location.href='/Lvscore_Advertiser_Maven/advertiserWeb/views/userCenter/recharge/index.html?'+escape('selectedNav=0&navPos=3')"},
	         {title:"账户安全",href:"javascript:location.href='/Lvscore_Advertiser_Maven/advertiserWeb/views/userCenter/accountSecurity/index.html?'+escape('selectedNav=0&navPos=4')"}	     
	     ],
		 "second":[
		     {title:"已上传广告",href:"javascript:location.href='/Lvscore_Advertiser_Maven/advertiserWeb/views/uploadAd/uploaded/index.html?'+escape('selectedNav=1&amp;navPos=0')"},
	         {title:"视频广告",href:"javascript:location.href='/Lvscore_Advertiser_Maven/advertiserWeb/views/uploadAd/videoAd/index.html?'+escape('selectedNav=1&amp;navPos=1')"},
	         {title:"图片广告",href:"javascript:location.href='/Lvscore_Advertiser_Maven/advertiserWeb/views/uploadAd/imageAd/index.html?'+escape('selectedNav=1&amp;navPos=2')"},	        
		 ], 
		 "third":[
		     {title:"已投放广告",href:"javascript:location.href='/Lvscore_Advertiser_Maven/advertiserWeb/views/adManagement/alreadyAd/index.html?'+escape('selectedNav=2&navPos=0')"},
	         {title:"投放视频广告",href:"javascript:location.href='/Lvscore_Advertiser_Maven/advertiserWeb/views/adManagement/vedioAd/index.html?'+escape('selectedNav=2&navPos=1')"},
	         {title:"投放图片广告",href:"javascript:location.href='/Lvscore_Advertiser_Maven/advertiserWeb/views/adManagement/pictureAd/index.html?'+escape('selectedNav=2&navPos=2')"},	        
		 ],
		 "fouth":[
		     {title:"按时间统计",href:"javascript:location.href='/Lvscore_Advertiser_Maven/advertiserWeb/views/adTotalPay/index.html?'+escape('selectedNav=3&navPos=0')"},
	         {title:"按广告名称统计",href:"javascript:location.href='/Lvscore_Advertiser_Maven/advertiserWeb/views/adTotalPay/totalByAdName.html?'+escape('selectedNav=3&navPos=1')"},
	         {title:"按渠道统计",href:"javascript:location.href='/Lvscore_Advertiser_Maven/advertiserWeb/views/adTotalPay/totalByAdContent.html?'+escape('selectedNav=3&navPos=2')"},	        
	         {title:"按广告形式统计",href:"javascript:location.href='/Lvscore_Advertiser_Maven/advertiserWeb/views/adTotalPay/totalByAdForm.html?'+escape('selectedNav=3&navPos=3')"},	        
//	         {title:"按行业属性统计",href:"javascript:location.href='/Lvscore_Advertiser_Maven/advertiserWeb/views/adTotalPay/totalByAdIndustry.html?'+escape('selectedNav=3&navPos=4')"},	        
		 ],
		 "fifth":[
		     {title:"按地域统计",href:"javascript:location.href='/Lvscore_Advertiser_Maven/advertiserWeb/views/bigData/index.html?'+escape('selectedNav=4&amp;navPos=0')"},
	         {title:"按设备载体统计",href:"javascript:location.href='/Lvscore_Advertiser_Maven/advertiserWeb/views/bigData/facility.html?'+escape('selectedNav=4&amp;navPos=1')"},
	         {title:"按系统统计",href:"javascript:location.href='/Lvscore_Advertiser_Maven/advertiserWeb/views/bigData/system.html?'+escape('selectedNav=4&amp;navPos=2')"},	        
		 ],
		 "sixth":[
		     {title:"广告列表",href:"javascript:location.href='/Lvscore_Advertiser_Maven/advertiserWeb/views/feedback/index.html?'+escape('selectedNav=5&amp;navPos=0')"},	         
		 ],
		 "seventh":[
		     {title:"收件箱",href:"javascript:location.href='/Lvscore_Advertiser_Maven/advertiserWeb/views/message/index.html?'+escape('selectedNav=6&amp;navPos=0')"},	         
		     {title:"发件箱",href:"javascript:location.href='/Lvscore_Advertiser_Maven/advertiserWeb/views/message/outbox.html?'+escape('selectedNav=6&amp;navPos=1')"},	         
		 ],
	 };
	 //获取url参数
	 _this.getFirstNav = function(name){
	     var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
         var r = unescape(window.location.search.substr(1)).match(reg);
         if(r!=null)return  unescape(r[2]); return null;
	 };	
	 //生成二级导航
	 _this.setSecondNav = function(navNum){
	     var navPos = _this.getFirstNav(navNum);
		 //一级导航图标更换；		 		    
		     $(".navBarBox").find("li").each(function(){	                		 
		         if($(this).index()!=navPos){
			         $(this).find("img").attr("src","/Lvscore_Advertiser_Maven/advertiserWeb/static/images/nav"+($(this).index()+1)+"1.png");
					 $(this).find("span").css("color","#afafaf");					 
			     }else{
			         $(this).find("img").attr("src","/Lvscore_Advertiser_Maven/advertiserWeb/static/images/nav"+($(this).index()+1)+"2.png");
			         $(this).find("span").css("color","#FF9933");
				 }
		     }); 
		 		 
		 var navList = new Array();
		 switch(navPos){
		     case "0":
			 navList = _this.firstNav.first;
			 break
			 case "1":
			 navList = _this.firstNav.second;
			 break
			 case "2":
			 navList = _this.firstNav.third;
			 break
			 case "3":
			 navList = _this.firstNav.fouth;
			 break
			 case "4":
			 navList = _this.firstNav.fifth;
			 break
			 case "5":
			 navList = _this.firstNav.sixth;
			 break	
			 case "6":
			 navList = _this.firstNav.seventh;
			 break	
		 }
		 for(var i = 0;i<navList.length;i++){		     
		     var addHtml = '<li><a href="'+navList[i].href+'">'+navList[i].title+'</a></li>'			 
		     $(".nav").find("ul").append(addHtml);					 
		 }		 		 
	 };	
	 _this.secondNavHover = function(pos){
	     var hoverPos = _this.getFirstNav(pos);
		 $(".nav").find("a").eq(hoverPos).addClass("linkFocus");
	 };
	 _this.init=function(navNum,pos){
	     _this.setSecondNav(navNum);
		 _this.secondNavHover(pos); 		 
	 };
};
function getQueryString(name) {
    var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i');
    var r = window.location.search.substr(1).match(reg);
    if (r != null) {
        return unescape(r[2]);
    }
    return null;
};
var modalDialog = {
     //广告关键字
     keywordModal:function(ele){	     
		 var font = "",box=$(".adKeyword");
		 box.empty();
		 ele.parent(".form-group").siblings(".form-group").each(function(i){		     
     		 if($(this).find("input").val()){
			     box.append("<span>"+$(this).find("input").val()+"</span>");	
			 }			    			 			
		 });		 		 
		 ele.next().trigger("click");
	 },
	 keywordModalEdit:function(ele){	     
		 $(".keywordModal").find("input").each(function(){
		     $(this).val("");
		 });
	     ele.parent("div.topTitle").next("div.adKeyword").find("span").each(function(i){
		     if($(this).html()){
			     console.log($(this).html());
			     $(".keywordModal").find("input").eq(i).val($(this).html());  
			 }
			 
		 });	     
	 },
	 //广告描述
	 adDescribe:function(ele){
	     var font = "",box=$(".adDescribe");
		 box.empty();
		 var content = ele.parent(".form-group").siblings("textarea.form-control").val();
		 $("div.itemAdGroup").find("div.adDescribe").html(content);		 
		 ele.next().trigger("click");    
	 },
	 adDescribeEdit:function(ele){
	     var dom = ele.parent("div.topTitle").next("div.adDescribe");
		 if(dom.text()){
		     $(".adDescribeModal").find("textarea").val(dom.text());
		 }
	 },
	//广告链接
	 adUrl:function(ele){
	     var font = "",box=$(".adUrl");
		 box.empty();
		 var content = ele.parent(".form-group").siblings("textarea.form-control").val();
		 $("div.itemAdGroup").find("div.adUrl").html(content);		 
		 ele.next().trigger("click");    
	 },
	 adUrlEdit:function(ele){
	     var dom = ele.parent("div.topTitle").next("div.adUrl");
		 if(dom.text()){
		     $(".adUrlModal").find("textarea").val(dom.text());
		 }
	 },
	 //广告属性标签
	 adTarget:function(ele){
	     var selectEle = $("ul.adTargetModalBox").find('[data-target="1"]');
	     if(selectEle.length > 0){  //选取了属性标签
		     $("#adClassify").empty();
		     selectEle.each(function(){
			     var src = $(this).attr("src");
				 var id = $(this).attr("data-id");
			     $("#adClassify").append("<li><img src='"+src+"' alt='' id='"+id+"'/></li>");
			 });
		 };
		 ele.next().trigger("click");    	    		 
	 },
	 adTargetEdit:function(ele){
	     var dom = ele.parent("div.topTitle").next("div.adTarget").find("#adClassify");
		 var ele = $("ul.adTargetModalBox").find('img');
		 ele.each(function(){   //清空选取记录
             $(this).attr("src","/Lvscore_Advertiser_Maven/advertiserWeb/static/images/genre_pic"+$(this).attr("data-id")+".png");
		 });
		 if(dom.find("li").length>0){
		     ele.attr("data-target","0");
		     dom.find("li").each(function(){
		         var id = $(this).find("img").attr("id");
				 var src = $(this).find("img").attr("src");
			     ele.each(function(){
			         if($(this).attr("data-id") == id){
				         $(this).attr("data-target","1");
						 $(this).attr("src",src);
						 return ;
				     }
			     });		 
		     });
		 }
	 },
};
var flashImg = {    
     leftClick:function(){
    	 var imgLength = Math.ceil($("ul.adCoverFilmsWrap").children().length/3); //每一套ul中li的个数（包括空图）
    	 var leftDis=parseInt($(".adCoverFilmsWrap").css("left"));
    	 var leftTo=imgLength*1035-1035;
    	 if(leftDis>=-1035){
    		 $(".adCoverFilmsWrap").css({"left":-leftTo+"px"});
    		 $(".adCoverFilmsWrap").animate({"left":"+=1035px"});
    	 }else{
    		 $(".adCoverFilmsWrap").animate({"left":"+=1035px"});
    	 }
	 },
	 rightClick:function(){
		 var imgLength = Math.ceil($("ul.adCoverFilmsWrap").children().length/3); //每一套ul中li的个数（包括空图）
		 var leftDis=parseInt($(".adCoverFilmsWrap").css("left"));
		 var leftTo=imgLength*1035-1035;
		 if(leftDis<=-leftTo){
    		 $(".adCoverFilmsWrap").css({"left":-1035+"px"});
    		 $(".adCoverFilmsWrap").animate({"left":"-=1035px"});
    	 }else{
    		 $(".adCoverFilmsWrap").animate({"left":"-=1035px"});
    	 };
	 },
	 delegateDom:"",
	 delegate:function(ele){
	     flashImg.delegateDom = ele;
	 },
	 isDelegate:function(ele){
	     flashImg.delegateDom.parent("li").remove();
		 ele.next().trigger("click"); 
	 },
	 editDom:"",
	 edit:function(ele){
	     flashImg.editDom = ele;
	 },
	 editUpload:function(e,ele){ 	 
         var fs = new FileReader(),files = e.files[0];
		 fs.readAsDataURL(files);		 		
		 fs.onload = function(event){		     
			 ele.parent("div.form-inline").next("div").find("img").attr("src",this.result);
		 }
	 },	 
	 upload:function(ele){
	     var src = ele.parent("div.form-group").prev("div").find("img").attr("src");		 
	     if(src){		    
		     flashImg.editDom.parent("div.adCoverFilmsBtnGroup").siblings("img").attr("src",src);
			 ele.next().trigger("click"); 
		 }
	 },
	 uploadPage:function(ele){
	     var src = ele.parent("div.form-group").prev("div").find("img").attr("src");		 
	     if(src){		    
		     flashImg.editDom.siblings("div.adCoverBox").children("img").attr("src",src);
			 ele.next().trigger("click"); 
		 }
	 },
	 setting:function(ele){
	     var src = flashImg.settingDom.parent("div.adCoverFilmsBtnGroup").siblings("img").attr("src");
		// console.log(src);
	     $("#adHomeimage").attr("src",src);
		 ele.next().trigger("click"); 
	 },
	 settingDom:"",
	 goSetting:function(ele){
	     flashImg.settingDom = ele;
	 }
};
var tableHandle = {  //表格
     //下拉列表        
     selectUi:function(){	     
	     $('[role="menu"]').find("li").bind("click",function(){
		     var _this = $(this);
			 var selectEle = $(this).children("a");
			 var showEle = $(this).parents(".btn-group").find(".btn").eq(0);
			 showEle.text(selectEle.text());			 			 
		 });
	 },	 
	 //全选/反选
	 checkAll:function(){ 
	     $(".table").find("th").first().children("[type='checkbox']").click(function(){
		     var _this = $(this);			 
			 _this.parents("tr").siblings("tr").find("[type='checkbox']").each(function(){
			     $(this)[0].checked = _this[0].checked;				
			 });
		 });
	 },
	 submit:function(url,method,data,callback){	//调用接口  //参数说明{url:"接口地址",method:"传送方式",data:"传送数据",callbalck:"回调函数名"}    
	     $.ajax({
		     dataType:"json",
			 url:url,
			 data:data,
			 type:method?method:"post",
			 timeout:"2000",
			 success:function(json){			    				 
				 callback(json);
			 }
		 });
	 },
	 //初始化
	 init:$(function(){
	     tableHandle.selectUi();
	     tableHandle.checkAll();	
	 })	
};
var sweetCookie = function(){
    var _this = this;
	 _this.setCookie = function(key,value,time){  //设置cookie;  time:毫秒        	 
        var exp = new Date();
        exp.setTime(exp.getTime() + time);
	     document.cookie = key +"="+ escape(value)+";expires=" + exp.toGMTString()+";path=/Lvscore_Advertiser_Maven/";
	 };
	 _this.getCookie = function(key){   //获取cookie;
	     var arr,reg=new RegExp("(^| )"+key+"=([^;]*)(;|$)");
        if(arr=document.cookie.match(reg))
        return unescape(arr[2]);
        else
        return null;
	 };
	 _this.delCookie = function(key){
	     var exp = new Date();
        exp.setTime(exp.getTime() - 1);
        var cval = _this.getCookie(key);		 
        if(cval!=null)
        document.cookie= key + "="+cval+";expires="+exp.toGMTString()+";path=/Lvscore_Advertiser_Maven/";		 
	 }
};
var msgUiBox = function(msg,callback){
    var html = '<div class="msgUiBox">'+
                    '<div class="msgUiBoxLayout"></div>'+
                    '<div class="msgUiBoxCont">'+
                        '<p>'+msg+'</p>'+			 
                    '</div>'+
               '</div>';
    //clear first,then append;
	 $("body").find(".msgUiBox").remove();
	 $("body").append(html);
	 //animation;				 
	 $(".msgUiBox").show();
	 $(".msgUiBox").stop().animate({top:260});
	 msgUiBox.fadeOut = function(){
	     $(".msgUiBox").fadeOut(1000);
		 if(callback){
		     callback();
		 }	     
	 };		     
	 setTimeout('msgUiBox.fadeOut()',2000);	 
};
//登出;
var loginOut = function(){    
     $.ajax({
	     type:"post",
		 url:"/Lvscore_Advertiser_Maven/loginOut",		 
		 success:function(data){
		     if(data){
			     window.location.href="/Lvscore_Advertiser_Maven/advertiserWeb/views/login/login.html";
			 }else{
			     console.log("error");
			 }			 
		 }
	 });
};
$(function(){  	  
     //加载头部;  
     var headerHtml = $.ajax({url:"/Lvscore_Advertiser_Maven/advertiserWeb/views/master/layout1/header.html",async:false});	 
	 $(".headerBox").html(headerHtml.responseText);
	 //广告属性标签选中状态;
	 $("ul.adTargetModalBox").find("img").click(function(){        
	     if($(this).parents("ul.adTargetModalBox").find("[data-target='1']").length < 6&&$(this).attr("data-target")=="0"){		     			 
			 var imageName = $(this).attr("src").split("images/");
			 var newImgName = imageName[1].split("_")[0]+"s_"+imageName[1].split("_")[1];
			 var src = imageName[0]+"images/"+newImgName;			 
			 $(this).attr("src",src);
			 $(this).attr("data-target","1");
		 }else{
		     if($(this).attr("data-target")=="0") return alert("最多选择6个属性标签！");
		     var imageName = $(this).attr("src").split("images/");
			 var newImgName = imageName[1].split("s_")[0]+"_"+imageName[1].split("_")[1];
			 var src = imageName[0]+"images/"+newImgName;			 
			 $(this).attr("src",src);
			 $(this).attr("data-target","1");
		     $(this).attr("data-target","0");			 
		 }	     
	 });        	 
	 if($(".content").length>0){
	     //轮询；实时更新未读信件数量;
//         var unReadMsgFun= setInterval("unReadMsgNums()",10000);
	 }	
});
//unReadMsg  更新未读信件数量;
var unReadMsgNums = function(){
     //console.log("in");
	 //console.log(test);
     $.ajax({
	     type:"post",
		 dataType:"json",
	     url:"/Lvscore_Advertiser_Maven/unReadMsg",		 
		 success:function(data){
		     //console.log(data);
		     if(data){			   
			     $("#unMsgNum").text("("+data.total+")");
	         }else{
			     console.log("error");
			     clearInterval(unReadMsgFun); 
			 }		 
		 }
	 });
}
//站内信;
var message = function(){ 
     $.ajax({
	     type:"post",
		 url:"/Lvscore_Advertiser_Maven/getInboxList",		 
		 success:function(data){
		     if(data){
			     window.location.href="/Lvscore_Advertiser_Maven/advertiserWeb/views/message/index.html?selectedNav=6&navPos=0";
			 }else{
			     console.log("error");
			 }			 
		 }
	 });
};



























